package com.txby.platformApi.component.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jck.mybatis.log.TxbyLogger;
import com.jck.mybatis.mapper.MybatisBaseDaoCRUD;
import com.txby.shopCore.component.service.SpringContextUtils;
import com.txby.shopCore.entity.GoodsOrders;
import com.txby.shopCore.entity.ShopsThirdPayOrder;
import com.txby.shopCore.entity.ShopsThirdPayOrderRecord;
import com.txby.shopOrders.component.service.ApiOrdersService;
import com.txby.shopOrders.component.util.QuartzFactoryUtil;
import com.txby.shopOrders.component.web.ApiOrdersController;

public class StartUpCancelOrderJobTask implements Job{
	private static Logger logger = LoggerFactory.getLogger(StartUpCancelOrderJobTask.class);
	public static String defaultJobName = "StartUpCancelOrderJobTask";
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		TxbyLogger.getLogger(TxbyLogger.class).log(TxbyLogger.getTxbyLoggerLevel(), JSON.toJSONString("启动订单校验"+ApiOrdersController.taskCancelOrderMnu+"分取消任务调度"+defaultJobName));
		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		MybatisBaseDaoCRUD mBaseDao=  (MybatisBaseDaoCRUD) dataMap.get("mBaseDao");
		ApiOrdersService aos=(ApiOrdersService) dataMap.get("aos");
		Map map=new HashMap();
		map.put("mBaseDao", mBaseDao);
		map.put("aos", aos);
		boolean result=false;
		String  sql="SELECT   b.outTradeNo,b.paymentTime,b.payStatus,b.id_ shopsThirdPayOrderId,go.id_ ordersId,go.ordersStatus,b.collectionMethod,TIMESTAMPDIFF(MINUTE,now(),date_add(go.createTime, interval "+ApiOrdersController.taskCancelOrderMnu+" minute)) residueMinute   from t_txby_sp_shopsThirdPayOrderRelation a,t_txby_sp_shopsThirdPayOrder b,t_txby_sp_goodsOrders go WHERE a.shopsThirdPayOrderId=b.id_  and  a.ordersId=go.id_ and  go.ordersStatus=0 ";
		List<Map> lst=mBaseDao.findBySql(sql, null, null);
		List<Long> tempordersId=new ArrayList<>();
		List<Long> tempshopsThirdPayOrderId=new ArrayList<>();
//		Date curDate=new Date();
		Set<String> outTradeNoList=new HashSet();
		for(Map m:lst){
			Long shopsThirdPayOrderId=new Long(m.get("shopsThirdPayOrderId").toString());
			Long ordersId=new Long(m.get("ordersId").toString());
			Long payStatus=new Long(m.get("payStatus").toString());
			Long ordersStatus=new Long(m.get("ordersStatus").toString());
			String  outTradeNo=m.get("outTradeNo").toString();
			Long collectionMethod=new Long(m.get("collectionMethod").toString());
			Integer residueMinute=new Integer(m.get("residueMinute").toString());
//			Date odCreateTime=(Date) m.get("odCreateTime");
		 
				if(residueMinute<=0){
					/*outTradeNoList.add(outTradeNo);
					if(!tempordersId.contains(ordersId)){
						GoodsOrders go=new GoodsOrders();
						go.setId_(ordersId);
						go.setCreateTime(null);
						go.setUpdateTime(new Date());;
						go.setOrdersStatus(4);
						mBaseDao.updateObject(go);
//						GoodsOrders go=(GoodsOrders) mBaseDao.findBySqlUniqueObject("select * from t_txby_sp_goodsOrders where id_="+ordersId+"", GoodsOrders.class, null);
//						go.setUpdateTime(new Date());;
//						go.setOrdersStatus(4);
//						mBaseDao.updateObject(go);
					}
					tempordersId.add(ordersId);*/
					aos.updateCancelOrderData(outTradeNo);
					QuartzFactoryUtil.getInstance().removeJobByCalendar(StartUpCancelOrderJobTask.defaultJobName+ordersId, StartUpCancelOrderJobTask.defaultJobName+ordersId);
				}else{
					QuartzFactoryUtil.getInstance().addJobByCalendar(StartUpCancelOrderJobTask.defaultJobName+ordersId,StartUpCancelOrderJobTask.defaultJobName+ordersId, StartUpCancelOrderJobTask.class,residueMinute, map);
				}
			 
		/*	if(payStatus==0){
				if(residueMinute<=0){
					
					if(collectionMethod==0){
						//查询最新商户订单状态进行更新，如果为未支付则取消，否则更新订单状态。
						Map jo= SpringContextUtils.getWxTradeUtil().doOrderClose(outTradeNo);
						ShopsThirdPayOrderRecord stpor =new ShopsThirdPayOrderRecord();
						stpor.setCreater("taskCancel");
						stpor.setUpdater("taskCancel");
						stpor.setOrdersStatus(0);
						stpor.setOutTradeNo(outTradeNo);
						stpor.setPayMethod(collectionMethod.intValue());
						stpor.setJsonDetail(jo==null?"":JSON.toJSONString(jo));
						int i=jo==null?0:mBaseDao.saveObject(stpor);
						
					}else{
						//查询最新商户订单状态进行更新，如果为未支付则取消，否则更新订单状态。
						JSONObject jo=SpringContextUtils.getAlipayTradeUtil().alipayTradeCancel(outTradeNo);
						ShopsThirdPayOrderRecord stpor =new ShopsThirdPayOrderRecord();
						stpor.setCreater("taskCancel");
						stpor.setUpdater("taskCancel");
						stpor.setOrdersStatus(0);
						stpor.setOutTradeNo(outTradeNo);
						stpor.setPayMethod(collectionMethod.intValue());
						stpor.setJsonDetail(jo==null?"":jo.toJSONString());
						int i=jo==null?0:mBaseDao.saveObject(stpor);
					}
					if(!tempshopsThirdPayOrderId.contains(shopsThirdPayOrderId)){
						ShopsThirdPayOrder stpo=new ShopsThirdPayOrder();
						stpo.setCreateTime(null);
						stpo.setUpdateTime(new Date());
						stpo.setId_(shopsThirdPayOrderId);
						stpo.setPayStatus(4);
						mBaseDao.updateObject(stpo);
					}
					
				}else{
					
					  QuartzFactoryUtil.getInstance().addJobByCalendar(StartUpCancelOrderJobTask.defaultJobName+shopsThirdPayOrderId,StartUpCancelOrderJobTask.defaultJobName+shopsThirdPayOrderId, StartUpCancelOrderJobTask.class,residueMinute, map);
				}
				tempshopsThirdPayOrderId.add(shopsThirdPayOrderId);
			}*/
		 
		}
		 
		QuartzFactoryUtil.getInstance().removeJobByCalendar(defaultJobName, defaultJobName);
	 
	}

	 
	
}
